package com.app.service.impl;

import com.app.common.AppException;
import com.app.dao.IDepartmentDao;
import com.app.dao.impl.DepartmentDao;
import com.app.pojo.Department;
import com.app.service.IDepartmentService;
import com.app.utils.UtilTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) @2020 fuguo.wang@logicsolutions.com.cn
 *
 * @author fuguo.wang
 * @version 1.0
 * @className DepartmentService
 * @date 2020-06-18 23:12
 * @description
 */
public class DepartmentService implements IDepartmentService {

	private final IDepartmentDao dao = new DepartmentDao();

	/**
	 * 查询全部数据集合
	 *
	 * @return
	 */
	@Override
	public List<Department> get() {
		List<Department> departments = dao.select();
		return UtilTools.isEmpty(departments) ? new ArrayList<>() : departments;
	}

	/**
	 * 根据id查询单条数据
	 *
	 * @param id 主键Id
	 * @return 数据对象
	 */
	@Override
	public Department get(Integer id) {
		if (UtilTools.isEmpty(id)) throw new AppException("查询详情，id不能为空！");
		Department department = dao.select(id);
		if(UtilTools.isEmpty(department)) throw new AppException("该部门已经被删除，请重新绑定！");
		return department;
	}

	/**
	 * 新增数据
	 *
	 * @param obj 数据对象
	 * @return 主键id
	 */
	@Override
	public Integer add(Department obj) {
		if (UtilTools.isEmpty(obj)) throw new AppException("新增对象不能为空！");
		if (UtilTools.isNotEmpty(obj.getId())) throw new AppException("新增id必须为空！");
		return dao.insert(obj);
	}

	/**
	 * 更新数据
	 *
	 * @param obj 更新数据对象
	 */
	@Override
	public void update(Department obj) {
		if (UtilTools.isEmpty(obj.getId())) throw new AppException("更新时，id不能为空！");
		dao.update(obj);
	}

	/**
	 * 删除数据，支持批量
	 *
	 * @param ids 删除的主键，例如 1,2,3,4  可以批量
	 */
	@Override
	public void delete(String ids) {
		if (UtilTools.isEmpty(ids)) throw new AppException("删除时，id不能为空！");
		dao.delete(ids);
	}
}
